package com.chuangjiangx.member.manager.web.web.basic.controller;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.response.UnderlineResponse;
import com.chuangjiangx.member.basic.ddd.query.dto.MbrConfigDTO;
import com.chuangjiangx.member.coupon.ddd.application.MbrCardApplication;
import com.chuangjiangx.member.coupon.ddd.application.command.CreateCardCommand;
import com.chuangjiangx.member.coupon.ddd.application.command.UpdateCardCommand;
import com.chuangjiangx.member.coupon.ddd.query.MbrCardQuery;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.web.web.basic.request.CreateMbrConfigRequest;
import com.chuangjiangx.member.manager.web.web.basic.request.ModifyMbrConfigRequest;
import com.chuangjiangx.member.manager.web.web.basic.response.MbrConfigResponse;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

/**
 * 会员配置信息
 *
 * @author wp
 * @date 2018-08-28 13:49
 **/
@Slf4j
@Api(value = "MbrConfigController", tags = "会员卡信息", description = "MbrConfigController")
@RestController
@RequestMapping(value = "/api/mbr/config", produces = "application/json")
public class MbrConfigController extends BaseController {

    private final MbrCardApplication mbrCardApplication;
    private final MbrCardQuery mbrCardQuery;

    @Autowired
    public MbrConfigController(MbrCardApplication mbrCardApplication, MbrCardQuery mbrCardQuery) {
        this.mbrCardApplication = mbrCardApplication;
        this.mbrCardQuery = mbrCardQuery;
    }

    /**
     * 创建会员卡配置
     *
     * @param session
     * @param request 会员卡基本信息接受参数
     * @return Response
     * @throws Exception
     */
    @PostMapping(value = "/create")
    @ApiOperation(value = "添加会员卡基础信息")
    @Login
    public Response createMbrCardConfig(@ApiIgnore HttpSession session, @Validated @RequestBody CreateMbrConfigRequest request) throws Exception {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        Long merchantId = user.getMerchantId();
        CreateCardCommand command = new CreateCardCommand();
        BeanUtils.convertBean(request,command,(re,co)->{
            co.setMerchantId(merchantId);
        });
        mbrCardApplication.createCard(command);
        return ResponseUtils.success();
    }

    /**
     * 查询会员卡配置
     *
     * @param session
     * @return Response
     * @throws Exception
     */
    @GetMapping(value = "/search-detail")
    @ApiOperation(value = "查询会员卡基础信息")
    @Login
    public UnderlineResponse<MbrConfigResponse> searchMbrCardConfigDetail(@ApiIgnore HttpSession session) throws Exception {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        MbrConfigDTO mbrConfigDTO = mbrCardQuery.searchCardDetail(user.getMerchantId());
        MbrConfigResponse response = new MbrConfigResponse();
        BeanUtils.convertBean(mbrConfigDTO,response,(dto,res)->{
            res.setGiftScore(0==dto.getGiftScore().intValue()?"":dto.getGiftScore().intValue()+"");
        });
        return (UnderlineResponse<MbrConfigResponse>)ResponseUtils.success(response);
    }

    /**
     * 修改会员卡配置
     *
     * @param session
     * @param request 会员卡基本信息接受参数
     * @return Response
     * @throws Exception
     */
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改会员卡配置")
    @Login
    public Response updataMbrConfig(@ApiIgnore HttpSession session, @Validated @RequestBody ModifyMbrConfigRequest request) {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        Long merchantId = user.getMerchantId();
        UpdateCardCommand command = new UpdateCardCommand();
        BeanUtils.convertBean(request,command);
        mbrCardApplication.updateCard(command);
        return ResponseUtils.success();
    }
}
